#include "WorkerManager.h"
#include "worker.h"
#include "employee.h"
#include "manager.h"
#include "boss.h"


WorkerManager::WorkerManager(){
    std::ifstream ifs;
    ifs.open(FILENAME, ios::in);
    char ch;
    ifs >> ch;
    if(!ifs.is_open() || ifs.eof()){
        cout<< "文件为空或文件不存在"<<endl;
        this->workerNum = 0;
        this->workerArray = NULL;
        return;
    }
    ifs.close();

    this->workerNum = this->countWorkerNum();
    // cout<< this->workerNum <<endl;
    initWorker();
    // for(int i = 0 ; i<this->workerNum; i++){
    //     cout<<this->workerArray[i]->workerID<<" "
    //         <<this->workerArray[i]->name<<" "
    //         <<this->workerArray[i]->departmentID<<endl;
    // }
}
WorkerManager::~WorkerManager(){
    if(this->workerArray != NULL){
        for(int i = 0; i < this->workerNum; i++){
            if(this->workerArray[i] != NULL){
                delete this->workerArray[i];
                this->workerArray[i] = NULL;
            }
        }
        delete[] this->workerArray;
        this->workerArray = NULL;
    }
}

void WorkerManager::showSystem(){   
    cout << "0.退出管理系统" <<endl;
    cout << "1.增加职工信息" <<endl;
    cout << "2.显示职工信息" <<endl;
    cout << "3.删除职工信息" <<endl;
    cout << "4.修改职工信息" <<endl;
    cout << "5.查找职工信息" <<endl;
    cout << "6.排序职工信息" <<endl;
    cout << "7.清空职工信息" <<endl;
}

void WorkerManager::exitSystem(){   //0.退出管理系统
    cout<< "系统结束使用"<<endl;
    system("pause");
    exit(0);
}

void WorkerManager::addWorker(){
    int newNum ;
    cout<<"输入添加员工的人数："<<endl;
    cin>>newNum;
    if(newNum > 0){
        Worker **newArray = new Worker *[newNum + this->workerNum ];
        if(this->workerArray != NULL){
            for(int i = 0; i < this->workerNum; i++){
                newArray[i] = this->workerArray[i];
            }
        }
        
        int workerID;
        string name;
        for(int i = 0; i<newNum; i++){
            cout<<"第"<< i+1 <<"个员工ID: "<<endl;
            cin>> workerID;
            cout<<"第"<< i+1 <<"个员工姓名："<<endl;
            cin>> name;

            Worker *worker;
            int flag = 0;
            cout<<"选择添加的第"<< i+1 <<"员工类别：1.普通员工，2.经理，3.Boss"<<endl;
            cin >> flag;
            switch(flag){
                case 1: 
                    worker = new Employee(workerID, 1, name);
                    break;
                case 2: 
                    worker = new Manager(workerID, 2, name);
                    break;
                case 3: 
                    worker = new Boss(workerID, 3, name);
                    break;   
            }
            newArray[this->workerNum + i] = worker;
        }
        delete[] this->workerArray;
        this->workerArray = newArray;
        this->workerNum += newNum; 
        cout<<"成功添加了"<< newNum <<"名员工"<<endl;
        this->saveFile();
    }
    else{
        cout<<"输入的数有误"<<endl;
        system("pause");
        system("cls");
    }
}
void WorkerManager::showWorker(){
    if(this->workerArray == NULL){
        cout<<"职工信息不存在或为空"<<endl;
        return;
    }
    for(int i=0; i<this->workerNum; i++ ){
        this->workerArray[i]->showWorkerInfo();
    }
}
void WorkerManager::saveFile(){
    std::ofstream ofs;
    ofs.open(FILENAME, ios::out);
    for(int i=0; i<this->workerNum; i++){
        ofs << this->workerArray[i]->workerID <<" "
            << this->workerArray[i]->name<< " "
            << this->workerArray[i]->departmentID<<endl;
    }
    ofs.close();
}
int WorkerManager::countWorkerNum(){
    int num = 0;
    std::ifstream ifs;
    ifs.open(FILENAME, ios::in);
    string buf;
    while(getline(ifs, buf)){
        num++;
    }
    ifs.close();
    return num;
}
void WorkerManager::initWorker(){
    std::ifstream ifs;
    ifs.open(FILENAME, ios::in);

    int id;
    string name;
    int did;

    int index = 0;
    Worker *worker;
    this->workerArray = new Worker *[this->workerNum];
    while(ifs>>id && ifs>>name && ifs>>did){
        switch(did){
            case 1:
                worker = new Employee(id, did, name); 
                break;
            case 2:
                worker = new Manager(id, did, name); 
                break;
            case 3:
                worker = new Boss(id, did, name); 
                break;
        }
        this->workerArray[index] = worker;
        index++;
    }
    ifs.close();
}

int WorkerManager::isExistWorker(int id){
    for(int i = 0; i < this->workerNum ; i++){
        if(this->workerArray[i]->workerID == id)
            return i;
    }
    if(this->workerArray == NULL) {
        cout<< "文件为空或文件不存在"<<endl;
        return -1;
    } 
    cout<< "id为"<< id <<"的员工不存在"<<endl;
    return -1;
}
void WorkerManager::deleteWorker(){
    cout<<"输入要删除的员工编号："<<endl;
    int id;
    cin >> id;
    if(isExistWorker(id) == -1) return;
    for(int i = isExistWorker(id); i<this->workerNum; i++){
        this->workerArray[i] = this->workerArray[i+1];
    }
    this->workerNum--;
    this->saveFile();
    cout<<"员工"<< id <<"已成功删除"<<endl;
}
void WorkerManager::alterWorker(){
    cout<<"输入要修改的员工编号："<<endl;
    int id;
    cin >> id;
    if(isExistWorker(id) == -1) return;

    int workerID;
    string name;
    int did;
    cout<<"员工ID: "<<endl;
    cin>> workerID;
    cout<<"员工姓名："<<endl;
    cin>> name;
    cout<<"员工类别：1.普通员工，2.经理，3.Boss"<<endl;
    cin >> did;

    int index = isExistWorker(id);
    Worker *worker;
    switch(did){
            case 1:
                worker = new Employee(id, did, name); 
                break;
            case 2:
                worker = new Manager(id, did, name); 
                break;
            case 3:
                worker = new Boss(id, did, name); 
                break;
    }
    this->workerArray[index] = worker;
    this->saveFile();
    cout<<"员工已修改"<<endl;
}
void WorkerManager::sereachWorker(){
    cout<<"输入要查找的员工编号："<<endl;
    int id;
    cin >> id;
    if(isExistWorker(id) == -1) return;
    this->workerArray[isExistWorker(id)]->showWorkerInfo();

}
void WorkerManager::randWorker(){
    Worker *temp;
    for(int i = this->workerNum ; i > 0; i--){
        for(int j = 1 ; j < i ; j++){
            if(this->workerArray[j]->workerID < this->workerArray[j-1]->workerID){
                temp = this->workerArray[j];
                this->workerArray[j] = this->workerArray[j-1];
                this->workerArray[j-1] = temp;
            }
        }
    }
    this->saveFile();
    cout<<"排序成功"<<endl;
}
void WorkerManager::removeWorkerList(){

    cout<<"是否真的清空？"<<endl;
    cout<<"Y:清空   N:保留"<<endl;
    char ch;
    cin >> ch;
    if(ch == 'Y'){
        std::ofstream ofs;
        ofs.open(FILENAME, ios::trunc);
        ofs.close();
        
        if(this->workerArray != NULL){
            for(int i = 0; i < this->workerNum; i++){
                if(this->workerArray[i] != NULL){
                    delete this->workerArray[i];
                    this->workerArray[i] = NULL;
                }
            }
            delete[] this->workerArray;
            this->workerArray = NULL;
        }
        cout<<"职工信息已清空"<<endl;
    }
    else {
        cout<<"数据已保留"<<endl;
        return;
    }

    
}